using System;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GameLib.GameSprite
{
    class BSprite : ASprite
    {
        #region Properties
        public SpriteEffects Flip
        {
            get { return flip; }
            set { flip = value; }
        }
        SpriteEffects flip = SpriteEffects.None;

        public Point CurrentFrame
        {
            get { return currentFrame; }
            set 
            { 
                currentFrame = value;
                UpdateFrame();
            }
        }
        Point currentFrame;

        public Point FrameSize
        {
            get { return frameSize; }
            protected set { frameSize = value; }
        }
        protected Point frameSize;
        #endregion

        #region Bound Info
        public Rectangle Bound
        {
            get { return bound; }
        }

        public Point BoundPosition
        {
            get { return new Point(bound.X, bound.Y); }
        }
       
        public int BoundPositionX
        {
            get { return bound.X; }
            set 
            {
                bound.X = value; 
            }
        }

        public int BoundPositionY
        {
            get { return bound.Y; }
            set 
            {
                bound.Y = value;
            }
        }

        public int BoundWidth
        {
            get { return bound.Width; }
            set
            {
                bound.Width = value;
            }
        }

        public int BoundHeight
        {
            get { return bound.Height; }
            set
            {
                bound.Height = value;
            }
        }

        private Rectangle bound;
        #endregion

        public BSprite(Texture2D texture, Vector2 pos, Point currentFrame, Point frameSize)
            : base(texture, pos)
        {
            this.currentFrame = currentFrame;
            this.frameSize = frameSize;
            bound = new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y);
        }

        public BSprite(Texture2D texture, Vector2 pos)
            : base(texture, pos)
        {
            this.currentFrame = new Point(0, 0);
            this.frameSize = new Point(texture.Width, texture.Height);
            bound = new Rectangle(0, 0, frameSize.X, frameSize.Y);
        }

        public void UpdateFrame()
        {
            bound.X = currentFrame.X * frameSize.X;
            bound.Y = currentFrame.Y * frameSize.Y;
        }

        public bool NextFrameX()
        {
            if ((currentFrame.X + 1) * frameSize.X < Texture.Width)
            {
                currentFrame.X += 1;
                bound.X = currentFrame.X * frameSize.X;
                bound.Y = currentFrame.Y * frameSize.Y;
                return true;
            }
            else
            {
                return false;
            }
            
        }

        public bool NextFrameY()
        {
            if ((currentFrame.Y + 1) * frameSize.X < Texture.Height)
            {
                currentFrame.Y += 1;
                bound.X = currentFrame.X * frameSize.X;
                bound.Y = currentFrame.Y * frameSize.Y;
                return true;
            }
            else
            {
                return false;
            }
        }

        public void SetBoundPositionX(bool isAdd, int value)
        {
            if (isAdd)
            {
                bound.X += value;
            }
            else
            {
                bound.X -= value;
            }
        }

        public void SetBoundPositionY(bool isAdd, int value)
        {
            if (isAdd)
            {
                bound.Y += value;
            }
            else
            {
                bound.Y -= value;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture,
                             Position,
                             bound,
                             Color.White,
                             0,
                             Vector2.Zero,
                             1f,
                             flip,
                             0);
        }

        public override void Draw(SpriteBatch spriteBatch, Vector2 vPos)
        {
            spriteBatch.Draw(Texture,
                             vPos,
                             bound,
                             Color.White,
                             0,
                             Vector2.Zero,
                             1f,
                             flip,
                             0);
        }
    }
}
